/**
 * @file InitDataBase.java
 * Projet eCOM 2010 (Miage Descartes)
 * @author Doido Sébastien
 * @version 2
 * @brief Définition du programme de chargement des données de base
 */
package ecom.client;

import ecom.beans.*;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;

/**
 * @brief Classe d'initialisation de la base de données
 * @author Doido Sébastien
 */
public class InitDataBase {

    /**
     * @brief Interface du bean d'administration distant
     */
    @EJB
    private static EcomAdminRemote ecomAdminBean = null;
    private static UserTransaction utx;

    /**
     * @brief Constructeur du programme d'initialisation de la base de données
     * @param args
     */
    public InitDataBase(String[] args) {
        try {
            InitialContext ic;
            ic = new InitialContext();
            utx = (UserTransaction) ic.lookup("UserTransaction");
            ecomAdminBean = (EcomAdminRemote) ic.lookup(EcomAdminRemote.class.getName());
        } catch (NamingException ex) {
            Logger.getLogger("Impossible de crer le contexte : " + ex);
        }
    }

    /**
     * @brief Lancement du programme d'alimentation de la base de données
     * @param args
     * @throws java.lang.Exception
     */
    public static void main(String[] args) throws Exception {
        InitDataBase db = new InitDataBase(args);
        db.run();
    }

    /**
     * @brief Chargement des données
     * @throws java.lang.Exception
     */
    private void run() throws Exception {
        try {
            //utx.begin();
            int accountId1 = ecomAdminBean.createAccount("DOIDO", 200.00, "doido", "pass_doido", "Sebastien", "50 rue du pot 91300 Massy", "08/07/1987");
            int accountId2 = ecomAdminBean.createAccount("MUHLEMANN", 2400.00, "muhlemann", "pass_muhlemann", "Adrien", "18 boulevard Jean Jaures", "06/08/1986");
            int accountId3 = ecomAdminBean.createAccount("COIFFE", 500.00, "coiffe", "pass_coiffe", "Florion", "50 rue Oui Oui 75011 Paris", "03/01/1986");
            int accountId4 = ecomAdminBean.createAccount("TESTARD", 100.00, "testard", "pass_testard", "Bruno", "8 rue de la chute 75015 Paris", "01/02/1985");
            int accountId5 = ecomAdminBean.createAccount("BARBE", 350.00, "barbe", "pass_barbe", "Benjamin", "17 rue de la chouette 75011 Paris", "01/02/1988");
            int accountId6 = ecomAdminBean.createAccount("HUANG", 650.00, "huang", "pass_huang", "Magalie", "1 rue du Hibou Gris 75004 Paris", "11/05/1985");
            int accountId7 = ecomAdminBean.createAccount("CUISIN", 1670.00, "cuisin", "pass_cuisin", "Zoe", "13 bd Voltaire 75011 Paris", "01/02/1986");
            int accountId8 = ecomAdminBean.createAccount("FOURNEROT", 4532.00, "fournerot", "pass_fournerot", "Thomas", "36 rue de Verdun 78000 Versailles", "01/02/1987");
            int accountId9 = ecomAdminBean.createAccount("DELSOL", 2330.00, "delsol", "pass_delsol", "Romain", "10 rue du nem 75001 Paris", "01/02/1986");

            int productStoreId1 = ecomAdminBean.createProductStore(accountId7, "Darty", "Grenoble");
            int productStoreId2 = ecomAdminBean.createProductStore(accountId8, "Boulanger", "Lyon");
            int productStoreId3 = ecomAdminBean.createProductStore(accountId9, "BHV", "Paris");
            int productStoreId4 = ecomAdminBean.createProductStore(accountId9, "BUT", "Lyon");

            int categ1 = ecomAdminBean.createProductCategory("Produit entretien");
            int categ2 = ecomAdminBean.createProductCategory("Petit electroménager");
            int categ3 = ecomAdminBean.createProductCategory("Mon bric à brac");

            int product1 = ecomAdminBean.createProduct("Plumeau", "desc", categ3);
            int product2 = ecomAdminBean.createProduct("Savon_noir", "desc", categ1);
            int product3 = ecomAdminBean.createProduct("Aspirateur", "desc", categ2);
            int product4 = ecomAdminBean.createProduct("Fer_a_repasser", "desc", categ2);
            int product5 = ecomAdminBean.createProduct("Eponge", "desc", categ1);
            int product6 = ecomAdminBean.createProduct("Desinsectiseur_Inox_40w", "desc", categ2);
            int product7 = ecomAdminBean.createProduct("Planche_a_repasser", "desc", categ3);

            ecomAdminBean.createStockLine(productStoreId1, product1, 12, 13.0);
            ecomAdminBean.createStockLine(productStoreId1, product2, 4, 4.0);
            ecomAdminBean.createStockLine(productStoreId1, product4, 14, 29.0);
            ecomAdminBean.createStockLine(productStoreId1, product5, 100, 2.0);
            ecomAdminBean.createStockLine(productStoreId1, product6, 9, 208.0);
            ecomAdminBean.createStockLine(productStoreId2, product2, 50, 6.90);
            ecomAdminBean.createStockLine(productStoreId2, product3, 20, 99.0);
            ecomAdminBean.createStockLine(productStoreId2, product4, 7, 23.0);
            ecomAdminBean.createStockLine(productStoreId2, product5, 86, 2.0);
            ecomAdminBean.createStockLine(productStoreId2, product6, 3, 180.0);
            ecomAdminBean.createStockLine(productStoreId3, product1, 20, 8.0);
            ecomAdminBean.createStockLine(productStoreId3, product2, 37, 3.40);
            ecomAdminBean.createStockLine(productStoreId3, product3, 20, 88.0);
            ecomAdminBean.createStockLine(productStoreId3, product4, 10, 50.0);
            ecomAdminBean.createStockLine(productStoreId3, product5, 32, 2.5);
            ecomAdminBean.createStockLine(productStoreId3, product6, 7, 185.0);
            ecomAdminBean.createStockLine(productStoreId4, product1, 2, 12.0);
            ecomAdminBean.createStockLine(productStoreId4, product2, 23, 5.50);
            ecomAdminBean.createStockLine(productStoreId4, product3, 20, 99.0);
            ecomAdminBean.createStockLine(productStoreId4, product4, 7, 34.50);
            ecomAdminBean.createStockLine(productStoreId4, product6, 3, 162.0);
            ecomAdminBean.createStockLine(productStoreId4, product7, 8, 24.90);

            //System.out.println("Before Commit");
            //utx.commit();
            System.out.println("-- Data injected with success --");

        } catch (Exception e) {
            System.err.println("InitDataBase program get an exception " + e);
            utx.rollback();
            System.exit(2);
        }
        System.out.println("End DB-Client");
    }
}
